package com.cictec.middleware.gps.engine.position.handler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cictec.middleware.commons.spring.SpringContextHolder;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.PropertiesUtils;
import com.cictec.middleware.core.entity.WarnMsg;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.exception.BusPositionHandlerException;
import com.cictec.middleware.gps.engine.exception.BusPositionWarnHandlerException;
import com.cictec.middleware.gps.engine.position.BusPositionHandler;
import com.cictec.middleware.gps.engine.position.handler.alarms.WarnHandler;

/**
 * 报警处理
 * @author ql
 */
@Component
public class BusPositionAlarmsHandler implements BusPositionHandler {

	/** 报警类型业务实现类集合 */
	@Autowired
	private Map<String, WarnHandler> warnHandler = new HashMap<String, WarnHandler>();
	
	private Map<String, WarnHandler> handlers = new HashMap<String, WarnHandler>();
	/** 需要处理的报警类型集合 */
	private static List<String> needHandleWarnList = null;
	
	public void init(){
		if(needHandleWarnList == null){
			String warntypes = PropertiesUtils.getString("system.parameter.warn.type", null);
			if(!PMSUtils.isEmpty(warntypes)){
				needHandleWarnList = new ArrayList<String>(Arrays.asList(warntypes.split(",")));
			}else{
				needHandleWarnList = new ArrayList<String>();
			}
		}
		

		for (Entry<String, WarnHandler> handler : warnHandler.entrySet()) {
			handlers.put(handler.getValue().getWarnType(), handler.getValue());
		}
	}
	
	@Override
	public void onHandler(BusInstance busInstance) throws BusPositionHandlerException {
		/*
		 * 手动报警				Constants.VAL_BUS_STATUS_6
		 * 偏线报警				Constants.VAL_BUS_STATUS_7
		 * 超速报警				Constants.VAL_BUS_STATUS_8
		 * 停车报警				Constants.VAL_BUS_STATUS_9
		 * 回场报警				Constants.VAL_BUS_STATUS_10
		 * 提前发车报警			Constants.VAL_BUS_STATUS_11
		 * 延迟发车报警			Constants.VAL_BUS_STATUS_12
		 * 压站报警				Constants.VAL_BUS_STATUS_13
		 * 甩站报警				Constants.VAL_BUS_STATUS_14
		 * 下一趟发车时刻预警		Constants.VAL_BUS_STATUS_20
		 */
		try {
			warnHandler(busInstance);
		} catch (BusPositionWarnHandlerException e) {
			e.printStackTrace();
		}
	}
	
	private void warnHandler(BusInstance busInstance) throws BusPositionWarnHandlerException{
		
		StringBuffer allAlarmsStr = new StringBuffer("");
		for (String warnType : needHandleWarnList) {
			if(handlers.containsKey(warnType)){
				// 报警处理
//				System.out.println("处理："+handlers.get(warnType));
				handlers.get(warnType).onHandler(busInstance);

				// 报警处理前端数据封装
				// 针对手动报警、超速（设备上传的）报警直接封装就可以
				attachWarn(busInstance, allAlarmsStr, warnType);
			}
			
		}
		busInstance.setAllAlarms(allAlarmsStr.toString());
	}

	/**
	 * 组装报警信息
	 * @param busInstance
	 * @param allAlarmsStr
	 * @param warnType
	 */
	private void attachWarn(BusInstance busInstance, StringBuffer allAlarmsStr,  String warnType) {
		WarnMsg warn = busInstance.getWarnsByWarnType(warnType);
		if (warn != null) {
			if (!warn.isClosed()) {
				allAlarmsStr = allAlarmsStr.length() == 0 ? allAlarmsStr.append(warnType) : allAlarmsStr.append("," + warnType);
			}
		}
	}
	
	
	
	
	
	

}
